﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class GoalInfo : Form
    {
        private int clubId = 0;
        private int gameId = 0;

        public GoalInfo(int clId,int gmId)
        {
            InitializeComponent();
            clubId = clId;
            gameId = gmId;
        }

        private void GoalInfo_Load(object sender, EventArgs e)
        {
            UpdatePlayerList();
        }

        private void UpdatePlayerList()
        {

            dgvPlayers.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getPlayersOfClub]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvPlayers.Rows.Add(dataReader["id"], dataReader["photo"], dataReader["name"], dataReader[3], dataReader[4]);
            }
            dataReader.Close();
            conn.Close();

            dgvPlayers.ClearSelection();

        }

        private void setSaveState() {

            if (nudMinute.Value > 0)
            {
                if (dgvPlayers.SelectedRows.Count > 0)
                {
                    save.Enabled = true;
                }
                else
                {
                    save.Enabled = false;
                }
            }
        }

        private void dgvPlayers_SelectionChanged(object sender, EventArgs e)
        {
            setSaveState();
        }

        private void save_Click(object sender, EventArgs e)
        {
            int plId = Convert.ToInt32(dgvPlayers.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[addGoal]";
            myCommand.Parameters.Add("@fk_club", SqlDbType.Int).Value = clubId;
            myCommand.Parameters.Add("@fk_game", SqlDbType.Int).Value = gameId;
            myCommand.Parameters.Add("@fk_player", SqlDbType.Int).Value = plId;
            myCommand.Parameters.Add("@minute", SqlDbType.Int).Value = nudMinute.Value;
            myCommand.ExecuteScalar();
            conn.Close();

            this.Close();
        }

        private void nudMinute_ValueChanged(object sender, EventArgs e)
        {
            setSaveState();
        }
    }
}
